package com.easywan.core;



import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import com.easywan.exception.RuleErrorException;
import com.easywan.pojo.IResult;
import com.easywan.vo.BaseManagerUser;
import com.easywan.vo.BaseUsers;

import net.sf.json.JSONObject;

public class BaseController {
	public String toJSON(IResult buildResult) {
		return JSONObject.fromObject(buildResult).toString();
	}
	public String getString(HttpServletRequest request, String string) {
		String result = request.getParameter(string);
		return result;
	}
	public String getPwd(HttpServletRequest request, String string) {
		String result = request.getParameter(string);
		
		result = EasyWanDES.DES(result);
		
		return result;
	}
	public String getPwd(HttpServletRequest request) {
		String result = getPwd(request,"pwd");
		return result;
	}
	/**
	 * 手机号规则
	 * @param request
	 * @return
	 */
	public String getMobile(HttpServletRequest request) {
 		String result = request.getParameter("mobile");
		if(result == null || result == "" || result.length() != 11){
			throw new RuleErrorException("手机号错误!");
		}
		int first = Integer.valueOf(result.charAt(0) - '0');
		if(first != 1){
			throw new RuleErrorException("手机号错误!");
		}
		for(int i = 1; i < 11; i ++){
			int check = Integer.valueOf(result.charAt(i) - '0');
			if(check > 9 || check < 0){
				throw new RuleErrorException("手机号错误!");
			}
		}
		return result;
	}
	
	public void setUserInfo(HttpServletRequest request,BaseManagerUser user) {
		request.getSession().setAttribute("manager", user);
	}
	public void setUserInfo(HttpServletRequest request,BaseUsers users) {
		request.getSession().setAttribute("users", users);
	}
	public BaseUsers  getUserInfo(HttpServletRequest request,String msg) {
		return (BaseUsers) request.getSession().getAttribute("users");
	}
	public BaseManagerUser  getUserInfo(HttpServletRequest request) {
		return (BaseManagerUser) request.getSession().getAttribute("manager");
	}
	public Integer getInt(HttpServletRequest request,String name){
		Integer result = -1;
		String resultString = getString(request, name);
		try{
			result = Integer.valueOf(resultString);
		}catch(Exception e){
			result = -1;
		}
			
		return result;
	}
	public void paramsNotNull(Object ...ps){
		for(int i =0 ; i < ps.length; i++){
			if(ps[i] instanceof String){
				if(ps[i] == null || ps[i].toString().trim() == ""){
					throw new RuntimeException("参数未填写完全！");
				}
			}else if(ps[i] instanceof Integer){
				if(ps[i] == null || (Integer)(ps[i]) == -1){
					throw new RuntimeException("参数未填写完全！");
				}
			}else {
				if(ps[i] == null){
					throw new RuntimeException("参数未填写完全！");
				}
			}
		}
	}
	public String getEmail(HttpServletRequest request){
		return getEmail(request,"email");
	}
	public String getEmail(HttpServletRequest request, String string) {
		String email = request.getParameter(string);
		Pattern p =  Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");//复杂匹配  
        Matcher m = p.matcher(email); 
        if(m.matches()){
        	return email;
        }else{
        	return null;
        }
	}
	protected Integer getUsersId(HttpServletRequest request) {
		BaseUsers users = getUserInfo(request,"");
		return users.getId();
	}
	/**
	 * 用作改版的时候
	 * @param string
	 * @return
	 */
	protected String toPath(String string) {
		return string;
	}
}
